package com.hongkzh.www.game.service;

import com.google.common.collect.Lists;
import com.hongkzh.www.adv.service.BaseService;
import com.hongkzh.www.conmon.BusinessException;
import com.hongkzh.www.conmon.DateUtil;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.StaticPublic;
import com.hongkzh.www.user.service.UserService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.List;

public class GameService  extends BaseService {

    public Page systemNotice(String loginUid, int pageNumber) {
           List<Record> list= Lists.newArrayList();
             Record r=Db.findFirst(Db.getSqlPara("game.getGameMonthNotice"));
             doImgPath(r);
                list.add(r);
        Page<Record> page2=Db.paginate(pageNumber,StaticPublic.PAGESIZE,Db.getSqlPara("game.getGameNotice"));

        for (Record r3:page2.getList()
                 ) {
                doImgPath(r3);
               list.add(r3);
            }
           Page<Record> page=new Page<>(list,pageNumber, StaticPublic.PAGESIZE,page2.getTotalPage(),page2.getTotalRow());
            return page;
    }

    public Page<Record> personProductList(String loginUid, int pageNumber, String title) {
        Kv cond=new Kv();
        if(StrKit.notBlank(title)){
            cond.set("title",title);
        }
        Page<Record> page= Db.paginate(pageNumber,StaticPublic.PAGESIZE,Db.getSqlPara("mediaShop.personProductList",cond));
        for (Record r:page.getList()){
            doImgPath(r);
        }
        return page;
    }
      @Before(Tx.class)
    public Integer addCart(String loginUid, String productId, String skuId, String number, String mediaUserId) {
        Record sku = Db.findFirst(Db.getSqlPara("game.getUserSkuinfo", skuId, productId));

        if (sku != null) {
            Record cart = Db.findFirst(Db.getSqlPara("game.selectCartBuskuId", loginUid, skuId, productId));
            if (cart != null) {
                Db.update(Db.getSqlPara("game.updateCart", loginUid, skuId, number, productId));
            } else {
                sku.set("id", IdGen.uuid());
                sku.set("number", number);
                sku.set("hk_user_id", mediaUserId);
                sku.set("user_id", loginUid);
                Db.update(Db.getSqlPara("game.addCart", sku));
            }


        }
          return this.cartCount(loginUid);
    }

    public Integer cartCount(String loginUid) {
        return Db.findFirst(Db.getSqlPara("game.cartCount",loginUid)).getInt("count");
    }

    public void updateCart(String loginUid, String cartId, String number) {
        Db.update(Db.getSqlPara("game.updateCartById",loginUid,cartId,number));
    }

    public Integer deleteCartByid(String loginUid, String cartId) {
        String []cartids=cartId.split(",");
        for(String c:cartids) {
            Db.update(Db.getSqlPara("game.deleteCartByid", loginUid, c));
        }
        return this.cartCount(loginUid);
    }

    public List<Record> cartList(String loginUid) {
        List<Record> rs=Db.find(Db.getSqlPara("game.selectCartList",loginUid));
        for (Record r:rs
             ) {
            doImgPath(r);
        }
        return rs;
    }

    public List<Record> couponList(String loginUid, int isPay) {
        if(isPay==1) {
            // 减少乐币
            SqlPara sqlPara = Db.getSqlPara("user.reduceIntegral", 10, loginUid);
            int i = Db.update(sqlPara);
            if (i == 0) {
                throw new BusinessException("乐币不够");
            }
        }
        List<Record> rs=Db.find(Db.getSqlPara("game.couponList"));
        for (Record r:rs
             ) {
            doImgPath(r);
        }
        return rs;
    }

    public Record getCouponByCouponId(String couponId) {
        SqlPara sqlPara=Db.getSqlPara("game.getCouponByCouponId",couponId);
        Record record=Db.findFirst(sqlPara);
        doImgPath(record);
        List<Record> imgs=Db.find(Db.getSqlPara("shop.getMediaProductImagesById",record.getStr("productId")));
        for (Record r:imgs
             ) {
            doImgPath(r);
        }
        record.set("imgs",imgs);
        return record;
    }

    public List<Record> getAllshop() {
        SqlPara sqlPara=Db.getSqlPara("game.getAllshop");
        List<Record> record=Db.find(sqlPara);
        for (Record r:record
             ) {
            doImgPath(r);
        }
        return record;
    }
    @Before(Tx.class)
    public Record rentShop(String loginUid, int snumber) {
        Record record=Db.findFirst(Db.getSqlPara("game.getShopBySnumber",snumber));
        if(StrKit.isBlank(record.getStr("userId"))) {
            String orderNumber = IdGen.getOrderIdByUUId();
            Kv cond2 = Kv.by("id", IdGen.uuid());
            cond2.set("user_id", loginUid);
            cond2.set("state", "-1");
            cond2.set("orderNumber", orderNumber);
            cond2.set("shop_id", record.getStr("snumber"));
            cond2.set("rent", record.getStr("rent"));
            Db.update(Db.getSqlPara("game.saveShopOrder",cond2));
            Db.update(Db.getSqlPara("game.updateShop",cond2));
            return new Record().set("orderNumber", orderNumber).set("rent", record.getStr("rent"));
        }
        else {
            throw new BusinessException("已经被别人抢走了");
        }
    }
    @Before(Tx.class)
    public void payRentShop(String loginUid, String orderNumber) {
        Record r=Db.findFirst(Db.getSqlPara("game.getShopOrderByorderNumber",orderNumber,loginUid));
        Db.update(Db.getSqlPara("game.updateShopOrder",loginUid,orderNumber));
        Db.update(Db.getSqlPara("game.updateShopByState",loginUid,r.get("shopId"),DateUtil.addYear(1)));
        Db.update(Db.getSqlPara("game.updateShelves",loginUid,r.get("shopId")));
    }

    public Record getShopBySnumber(String loginUid, String snumber) {
        Record record=Db.findFirst(Db.getSqlPara("game.getShopInfo",snumber));
        doImgPath(record);
        record.set("isMy",0);
        if(record.getStr("userId").equals(loginUid)){
            record.set("isMy",1);
        }
        return record;
    }
    @Before(Tx.class)
    public void updateShopTitle(String loginUid, String snumber, String title) {
        Db.update(Db.getSqlPara("game.updateShopTitle",loginUid,snumber,title));
    }
    @Before(Tx.class)
    public void updateShopCategory(String loginUid, String snumber, String categoryId) {
        Db.update(Db.getSqlPara("game.updateShopCategory",loginUid,snumber,categoryId));
    }

    public List<Record> getShopCategoryList(String loginUid) {
        List<Record> records=Db.find(Db.getSqlPara("game.getShopCategoryList"));
        for (Record r:records
             ) {
            doImgPath(r);
        }
        return records;
    }

    public  Record  getMyShop(String loginUid) {
        Record record=Db.findFirst(Db.getSqlPara("game.getMyShop",loginUid));
        doImgPath(record);

        return record;
    }

    public Record myData(String loginUid) {
        Record r=Db.findFirst(Db.getSqlPara("game.myData",loginUid));
        if(StrKit.isBlank(r.getStr("bean")))
        {
            r.set("bean",0);
        }
        if(StrKit.isBlank(r.getStr("lobo")))
        {
            r.set("lobo",0);
        }
        return r;
    }

    public Record mySynthesis(String loginUid) {
        Record r=Db.findFirst(Db.getSqlPara("game.mySynthesis",loginUid));
        return r;
    }

    public Record synthesisLobo(String loginUid) {

        Record  r=Db.findFirst(Db.getSqlPara("game.getUserBean",loginUid));
         if(r==null||r.getInt("count")<1000){
             throw new BusinessException("乐豆不够");
         }
        List rs=Db.find(Db.getSqlPara("game.synthesisLobo",loginUid));
         if(rs!=null&&rs.size()>0){
             throw new BusinessException("当前有任务");
         }
         int integral=Db.findFirst(Db.getSqlPara("game.getSynthesisIntegral")).getInt("integral");
         Db.update(Db.getSqlPara("game.updateUserBean",loginUid));
         Db.update(Db.getSqlPara("game.saveUserSynthesis",IdGen.uuid(),loginUid,integral, DateUtil.addDate(1)));
          r=Db.findFirst(Db.getSqlPara("game.synthesisLobo",loginUid));
        return r;
    }
    @Before(Tx.class)
    public void synthesisLoboSuccess(String loginUid, String synthesisId) {
        Record r=Db.findFirst(Db.getSqlPara("game.synthesisLoboSuccess",loginUid,synthesisId));
         if(null==r){
             throw new BusinessException("未到时间或者已经合成");
         }
        success(loginUid,synthesisId);

    }
    private void success(String loginUid, String synthesisId){
       int i= Db.update(Db.getSqlPara("game.updateSynthesisLobo",loginUid,synthesisId));

       if(i>0) {
           Record r = Db.findFirst(Db.getSqlPara("game.getUserLobo", loginUid));
           if (r == null) {
               Db.update(Db.getSqlPara("game.saveUserLobo", IdGen.uuid(), loginUid));
           } else {
               Db.update(Db.getSqlPara("game.updateUserLobo", loginUid));
           }
       }

    }
    @Before(Tx.class)
    public Record accelerateSynthesisLobo(String loginUid, String synthesisId)   {
        Record r=Db.findFirst(Db.getSqlPara("game.mySynthesis",loginUid));
        UserService us= Enhancer.enhance(UserService.class);
        us.reduceIntegral(loginUid,r.getInt("integral"),"加速合成");
        //success(loginUid,synthesisId);
        Db.update(Db.getSqlPara("game.updateSynthesisEndtime",loginUid,synthesisId));
        r=Db.findFirst(Db.getSqlPara("game.synthesisLobo",loginUid));
        return r;
    }
    @Before(Tx.class)
    public Record gameRechargeOrder(String loginUid, int integral) {
        String orderNumber=IdGen.getOrderIdByUUId();
        Kv cond2=Kv.by("id",IdGen.uuid());
        cond2.set("user_id",loginUid);
        cond2.set("state","-1");
        cond2.set("orderNumber",orderNumber);
        cond2.set("integral",integral);
        Db.update(Db.getSqlPara("mediaShop.saveGameRechargeOrder",cond2));
        return new Record().set("orderNumber",orderNumber).set("money",integral/100);
    }

    public List<Record> advList() {
        List<Record> list=Db.find(Db.getSqlPara("game.advList"));
        for (Record r:list
             ) {
            doImgPath(r);
        }


        return list;
    }
}
